package com.gas.gm.pdu.processor;

import com.gas.gm.model.Account;
import com.gas.gm.pdu.handler.BindRequestHandler;
import com.gas.gm.pdu.handler.RequestHandler;
import com.gas.gm.pdu.session.GasGmSession;
import com.gas.gm.util.GasFileMode;
import com.logica.smpp.Data;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.BindResponse;
import com.logica.smpp.pdu.Request;
import com.logica.smpp.pdu.Response;

public class BindRequestProcessor extends GasRequestProcessor{
	
	private RequestHandler handler;
	
	private boolean bound=false;
	
	private Account account=null;

	public BindRequestProcessor(GasGmSession session) {
		super(session);
		handler=new BindRequestHandler();
	}
	
	

	@Override
	public void processRequest(Request request) throws Exception {
		int commandStatus;
		Response response;
		int commandId=request.getCommandId();
		if(!bound){
			if(commandId==Data.BIND_RECEIVER||commandId==Data.BIND_TRANSMITTER||commandId==Data.BIND_TRANSCEIVER){
				commandStatus=handler.validateBindRequest((BindRequest) request);
				if(commandStatus==0){
					BindResponse bindresp=(BindResponse)request.getResponse();
					bindresp.setSystemId(GasFileMode.SYSTEM_ID);
					session.sendPdu(bindresp);
					bound=true;
					account=(Account) handler.getSerializableObject();
				}else{
					response=request.getResponse();
					response.setCommandStatus(request.getCommandStatus());
					session.sendPdu(response);
					session.stop();
				}
			}else{
				if(request.canResponse())
				{
					response=request.getResponse();
					response.setCommandStatus(Data.ESME_RINVBNDSTS);
					session.sendPdu(response);
				}
				session.stop();
			}
		}
		
	}

	


	public Account getValidAccount() {
		return account;
	}



	@Override
	public void processResponse(Request request) {
		Response response=request.getResponse();
		session.sendPdu(response);
	}



	
	
	

}
